import { createWebHistory, createRouter } from "vue-router";
import Layout from "@/layout";
import useUserStore from "@/store/modules/user";

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * roles: ['admin', 'common']       // 访问路由的角色权限
 * permissions: ['a:a:a', 'b:b:b']  // 访问路由的菜单权限
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */

// 公共路由
export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
      // 审核
      {
        path: "/designerMGT/detailPlanner/:designerId",
        component: () => import("@/views/decor/planner/detailPlanner.vue"),
        name: "detailPlanner",
        meta: {
          title: "设计师审核",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/designAgency/auditDesignAgencyDeco/:organizationId/:origin",
        component: () =>
          import("@/views/decor/designAgency/auditDesignAgencyDeco.vue"),
        name: "auditDesignAgencyDeco",
        meta: {
          title: "机构审核",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/schemManager/detailSchem/:schemeId",
        component: () => import("@/views/decor/schem/detailSchem.vue"),
        name: "detailSchem",
        meta: {
          title: "设计方案审核",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/MaterialStore/auditMaterial/:materialSupplierId/:origin",
        component: () =>
          import("@/views/decor/MaterialStore/auditMaterial.vue"),
        name: "auditMaterial",
        meta: {
          title: "材料商审核",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/materialAudit/auditMaterialAudit/:materialId/:origin",
        component: () =>
          import("@/views/decor/materialAudit/auditMaterialAudit.vue"),
        name: "auditMaterialAudit",
        meta: {
          title: "材料审核",
          icon: "",
          breadcrumb: false,
        },
      },
      // 管理端操作
      {
        path: "/millwright/editMillwright/:designerId?",
        component: () =>
          import("@/views/managerMGT/millwright/editMillwright.vue"),
        name: "editMillwright",
        meta: {
          title: "设计师操作",
          icon: "",
          breadcrumb: false,
        },
      },

      {
        path: "/designScheme/editDesignScheme/:schemeId?",
        component: () =>
          import("@/views/managerMGT/designScheme/editDesignScheme.vue"),
        name: "editDesignScheme",
        meta: {
          title: "设计方案操作",
          icon: "",
          breadcrumb: false,
        },
      },

      {
        path: "/designerAgency/editDesignerAgency/:organizationId?",
        component: () =>
          import("@/views/managerMGT/designerAgency/editDesignerAgency.vue"),
        name: "editDesignerAgency",
        meta: {
          title: "机构操作",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/materialBusiness/editMaterialBusiness/:materialSupplierId?",
        component: () =>
          import(
            "@/views/managerMGT/materialBusiness/editMaterialBusiness.vue"
          ),
        name: "editMaterialBusiness",
        meta: {
          title: "材料商操作",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/materialAuditting/editMaterialAuditting/:materialId?",
        component: () =>
          import(
            "@/views/managerMGT/materialAuditting/editMaterialAuditting.vue"
          ),
        name: "editMaterialAuditting",
        meta: {
          title: "材料操作",
          icon: "",
          breadcrumb: false,
        },
      },
      // 广告
      {
        path: "/advertisement/activeEdit/:campaignPageId?",
        component: () => import("@/views/decor/advertisement/activeEdit.vue"),
        name: "activeEdit",
        meta: {
          title: "活动页编辑",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/advertisement/editAdv/:origin/:adsId?",
        component: () => import("@/views/decor/advertisement/editAdv.vue"),
        name: "editAdv",
        meta: {
          title: "广告编辑",
          icon: "",
          breadcrumb: false,
        },
      },
      // 优惠券
      {
        path: "/couponMGT/editCoupon/:couponId?",
        component: () => import("@/views/couponMGT/editCoupon.vue"),
        name: "editCoupon",
        meta: {
          title: "优惠券编辑",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/couponMGT/detailCoupon/:couponId",
        component: () => import("@/views/couponMGT/detailCoupon.vue"),
        name: "detailCoupon",
        meta: {
          title: "优惠券详情",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/orderMGT/editOrder/:orderId/:origin",
        component: () => import("@/views/orderMGT/editOrder.vue"),
        name: "editOrder",
        meta: {
          title: "订单管理",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/remitMGT/editRemit/:remitId/:origin",
        component: () => import("@/views/remitMGT/editRemit.vue"),
        name: "editRemit",
        meta: {
          title: "打款管理",
          icon: "",
          breadcrumb: false,
        },
      },
      // 平台端
      {
        path: "/enterPages",
        component: () => import("@/views/platform/enterPages.vue"),
        name: "enterPages",
        meta: {
          title: "我要入驻",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/ordinaryUser/editOrdinaryUserInfo/:userId",
        component: () =>
          import("@/views/platform/ordinaryUser/editOrdinaryUserInfo.vue"),
        name: "editOrdinaryUserInfo",
        meta: {
          title: "个人信息编辑",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/designers/editDesigner/:origin",
        component: () => import("@/views/platform/designers/editDesigner.vue"),
        name: "editDesigner",
        meta: {
          title: "设计师信息修改",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/designers/editDesignerWork/:schemeId",
        component: () =>
          import("@/views/platform/designers/editDesignerWork.vue"),
        name: "editDesignerWork",
        meta: {
          title: "作品修改",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/designAgency/editDesignAgency/:origin",
        component: () =>
          import("@/views/platform/designAgency/editDesignAgency.vue"),
        name: "editDesignAgency",
        meta: {
          title: "个人信息编辑",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/designAgency/angencyDesignerSchem/:schemeId",
        component: () =>
          import("@/views/platform/designAgency/angencyDesignerSchem.vue"),
        name: "angencyDesignerSchem",
        meta: {
          title: "作品详情",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/materialCom/editMaterialCom/:origin",
        component: () =>
          import("@/views/platform/materialCom/editMaterialCom.vue"),
        name: "editMaterialCom",
        meta: {
          title: "个人信息编辑",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/designers/workDetail/:schemeId?",
        component: () => import("@/views/platform/designers/workDetail.vue"),
        name: "workDetail",
        meta: {
          title: "作品详情",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/designers/designerDetailInfo/:schemeId",
        component: () =>
          import("@/views/platform/designers/designerDetailInfo.vue"),
        name: "designerDetailInfo",
        meta: {
          title: "编辑作品详情",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/materialCom/materialDetail/:materialId?",
        component: () =>
          import("@/views/platform/materialCom/materialDetail.vue"),
        name: "materialDetail",
        meta: {
          title: "材料详情",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/materialCom/editMaterialInfo/:materialId",
        component: () =>
          import("@/views/platform/materialCom/editMaterialInfo.vue"),
        name: "editMaterialInfo",
        meta: {
          title: "材料修改",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/materialCom/materialDetailInfo/:materialId",
        component: () =>
          import("@/views/platform/materialCom/materialDetailInfo.vue"),
        name: "materialDetailInfo",
        meta: {
          title: "编辑材料详情",
          icon: "",
          breadcrumb: false,
        },
      },
      {
        path: "/designAgency/agencyDesignerDetail/:designerId",
        component: () =>
          import("@/views/platform/designAgency/agencyDesignerDetail.vue"),
        name: "agencyDesignerDetail",
        meta: {
          title: "设计师详情",
          icon: "",
          breadcrumb: false,
        },
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login.vue"),
    hidden: true,
  },
  {
    path: "/register",
    component: () => import("@/views/register.vue"),
    hidden: true,
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/error/404.vue"),
    hidden: true,
  },
  {
    path: "/401",
    component: () => import("@/views/error/401.vue"),
    hidden: true,
  },
  // 管理员
  {
    path: "",
    component: Layout,
    redirect: "/index",
    hidden: true,
    children: [
      {
        path: "/index",
        component: () => import("@/views/index.vue"),
        name: "Index",
        meta: {
          title: "首页",
          icon: "dashboard",
          affix: true,
          role: "Admin", // 仅管理员可见
        },
      },
    ],
  },
  // 设计师
  {
    path: "",
    component: Layout,
    redirect: "/designerHome",
    hidden: true,
    children: [
      {
        path: "/designerHome",
        component: () => import("@/views/platform/designers/designerHome.vue"),
        name: "designerHome",
        meta: {
          title: "个人中心",
          icon: "user",
          affix: true,
          role: "Designer", // 设计师
        },
      },
    ],
  },
  // 材料商
  {
    path: "",
    component: Layout,
    redirect: "/materialComHome",
    hidden: true,
    children: [
      {
        path: "/materialComHome",
        component: () =>
          import("@/views/platform/materialCom/materialComHome.vue"),
        name: "materialComHome",
        meta: {
          title: "个人中心",
          icon: "user",
          affix: true,
          role: "Material", // 材料商
        },
      },
    ],
  },
  // 设计机构
  {
    path: "",
    component: Layout,
    hidden: true,
    redirect: "/designAgencyHome",
    children: [
      {
        path: "/designAgencyHome",
        component: () =>
          import("@/views/platform/designAgency/designAgencyHome.vue"),
        name: "DesignAgencyHome",
        meta: {
          title: "个人中心",
          icon: "user",
          affix: true,
          role: "DesignOrganization", // 设计机构
        },
      },
    ],
  },
  // 普通用户
  {
    path: "",
    component: Layout,
    redirect: "/ordinaryUser",
    hidden: true,
    children: [
      {
        path: "/ordinaryUser",
        component: () =>
          import("@/views/platform/ordinaryUser/ordinaryUser.vue"),
        name: "OrdinaryUser",
        meta: {
          title: "个人中心",
          icon: "user",
          affix: true,
          role: "Common", // 普通用户可见
        },
      },
    ],
  },
  {
    path: "/user",
    component: Layout,
    hidden: true,
    redirect: "noredirect",
    children: [
      {
        path: "profile",
        component: () => import("@/views/system/user/profile/index.vue"),
        name: "Profile",
        meta: {
          title: "个人中心",
          icon: "user",
        },
      },
    ],
  },
];

// 动态路由，基于用户权限动态去加载
export const dynamicRoutes = [
  {
    path: "/system/user-auth",
    component: Layout,
    hidden: true,
    permissions: ["system:user:edit"],
    children: [
      {
        path: "role/:userId(\\d+)",
        component: () => import("@/views/system/user/authRole.vue"),
        name: "AuthRole",
        meta: {
          title: "分配角色",
          activeMenu: "/system/user",
          icon: "",
        },
      },
    ],
  },
  {
    path: "/system/role-auth",
    component: Layout,
    hidden: true,
    permissions: ["system:role:edit"],
    children: [
      {
        path: "user/:roleId(\\d+)",
        component: () => import("@/views/system/role/authUser.vue"),
        name: "AuthUser",
        meta: {
          title: "分配用户",
          activeMenu: "/system/role",
          icon: "",
        },
      },
    ],
  },
  {
    path: "/system/dict-data",
    component: Layout,
    hidden: true,
    permissions: ["system:dict:list"],
    children: [
      {
        path: "index/:dictId(\\d+)",
        component: () => import("@/views/system/dict/data.vue"),
        name: "Data",
        meta: {
          title: "字典数据",
          activeMenu: "/system/dict",
          icon: "",
        },
      },
    ],
  },
  {
    path: "/system/oss-config",
    component: Layout,
    hidden: true,
    permissions: ["system:ossConfig:list"],
    children: [
      {
        path: "index",
        component: () => import("@/views/system/oss/config.vue"),
        name: "OssConfig",
        meta: {
          title: "配置管理",
          activeMenu: "/system/oss",
          icon: "",
        },
      },
    ],
  },
  {
    path: "/tool/gen-edit",
    component: Layout,
    hidden: true,
    permissions: ["tool:gen:edit"],
    children: [
      {
        path: "index/:tableId(\\d+)",
        component: () => import("@/views/tool/gen/editTable.vue"),
        name: "GenEdit",
        meta: {
          title: "修改生成配置",
          activeMenu: "/tool/gen",
          icon: "",
        },
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_CONTEXT_PATH),
  //history: createWebHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return {
        top: 0,
      };
    }
  },
});
// 获取当前用户信息
function getCurrentUser() {
  return {
    role: useUserStore().roles,
  };
}
router.beforeEach((to, from, next) => {
  const user = getCurrentUser();
  if (to.meta.role) {
    if (from.path === to.path) {
      // 当前路径与目标路径相同，避免无限重定向
      return next();
    }
    // 根据角色进行重定向
    switch (user.role) {
      case "Designer":
        if (to.path !== "/designerHome") {
          return next({ path: "/designerHome" });
        }
        break;
      case "Material":
        if (to.path !== "/materialComHome") {
          return next({ path: "/materialComHome" });
        }
        break;
      case "DesignOrganization":
        if (to.path !== "/designAgencyHome") {
          return next({ path: "/designAgencyHome" });
        }
        break;
      case "Common":
        if (to.path !== "/ordinaryUser") {
          return next({ path: "/ordinaryUser" });
        }
        break;
      default:
        if (to.path !== "/index") {
          return next({ path: "/index" });
        }
        break;
    }
  }
  // 继续导航
  next();
});
export default router;
